package org.lyl.taobao.controller;

import org.lyl.taobao.api.ICommodityService;
import org.lyl.taobao.api.entity.Commodity;
import org.lyl.taobao.service.CommodityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/4/3 15:25
 */
@WebServlet("/checkCommodity")
public class CheckCommodityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ICommodityService iCommodityService = new CommodityServiceImpl();
        String check = req.getParameter("check");
        String userId = req.getParameter("userId");
        if (userId != null) {
            Integer id = Integer.valueOf(userId);
            List<Commodity> commodityList = iCommodityService.checkCommodityById(id);
            req.setAttribute("listMsg",commodityList);
            req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
            return;
        }
        if (check != null) {
            if (check.equals("deleteCommodity")) {
                int c_Id;
                String[] strings = req.getParameterValues("deleteCommodityById");
                if (strings != null) {
                    for (int i = 0; i < strings.length; i++) {
                        c_Id = Integer.valueOf(strings[i]);
                        iCommodityService.deleteCommodity(c_Id);
                    }
                }
                req.getRequestDispatcher("checkCommodity.jsp").forward(req,resp);
                return;
            } else if (check.equals("addCommodity")){
                req.getRequestDispatcher("addCommodity.jsp").forward(req,resp);
                return;
            }
        }
        String c_Id = req.getParameter("updateCommodityById");
        req.setAttribute("c_Id",c_Id);
        req.getRequestDispatcher("updateCommodity.jsp").forward(req,resp);
    }
}

